package tool;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

public class FileLoader {
	
	private FileConnection fc;
	
	public FileLoader(String path) throws IOException {
		fc = (FileConnection) Connector.open(path);

		if ((fc == null) || !fc.exists()) {
			fc.create();
		}
	}
	
	public String readContent() throws IOException {
		byte[] b = new byte[256];
		InputStream fis = fc.openInputStream();
		int length = fis.read(b, 0, 256);
		fis.close();
		if (length == -1) {
			return null;
		} else {			
			return new String(b, 0, length);
		}
	}

	public void overwriteContent(String newContent) throws IOException {
		fc.delete();
		fc.create();
		OutputStream fos = fc.openOutputStream();
		fos.write(newContent.getBytes());
		fos.close();
	}

	public void close() throws IOException {
		fc.close();
	}

	public void deleteFile() throws IOException {
		fc.delete();
	}
}
